package com.CST.service.impl.cb;

import com.CST.common.api.Response;
import com.CST.common.constant.Constants;
import com.CST.entity.cb.BdDepartment;
import com.CST.entity.cb.RuleAmortization;
import com.CST.entity.cb.selectInfo;
import com.CST.mapper.cst.cb.RuleAmortizationMapper;
import com.CST.service.cb.IRuleAmortizationService;
import com.CST.util.HttpUtil;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author ly
 * @since 2021-10-27
 */
@Service
@SuppressWarnings("all")
public class RuleAmortizationServiceImpl extends ServiceImpl<RuleAmortizationMapper, RuleAmortization> implements IRuleAmortizationService {


    @Override
    public List<RuleAmortization> getRuleAmortization(RuleAmortization entity) {
        return baseMapper.getRuleAmortization(entity);
    }

    @Override
    public RuleAmortization getRuleAmortizationById(RuleAmortization entity) {
        return baseMapper.getRuleAmortizationById(entity);
    }

    @Override
    public List<selectInfo> getYearMonth() {
        return baseMapper.getYearMonth();
    }

    @Override
    public List<BdDepartment> checkNoAmortizationDept(Integer year, Integer month) {
        return baseMapper.checkNoAmortizationDept(year,month);
    }


//    @Override
//    public Response addRuleAmortization(RuleAmortization entity) {
//        try {
//            Calendar cal = Calendar.getInstance();
//            Date currDate = new Date();
//            cal.setTime(currDate);
//            cal.add(Calendar.MONTH,-1);
//            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//            entity.setExtendField1(String.valueOf(cal.get(Calendar.YEAR)));
//            entity.setExtendField2(String.valueOf(cal.get(Calendar.MONTH) + 1));
//            entity.setCreatedOn(sdf.format(currDate));
//            entity.setUpdatedOn(sdf.format(currDate));
//            entity.setRuleCode("0");
//            String userName = HttpUtil.getRequestHeaderInfo(Constants.USER_NAME);
//            userName = userName == null || userName.isEmpty() ? "System" : userName;
//            entity.setCreatedBy(userName);
//            entity.setUpdatedBy(userName);
//            baseMapper.addRuleAmortization(entity);
//        } catch (Exception ex) {
//            return Response.error(String.join(":", "添加失败:", ex.getMessage()));
//        }
//        return Response.success("添加成功");
//    }


    @Override
    public Response addRuleAmortization(RuleAmortization entity) {
        try {
            Date currDate = new Date();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            entity.setCreatedOn(sdf.format(currDate));
            entity.setUpdatedOn(sdf.format(currDate));
            entity.setRuleCode("0");
            String userName = HttpUtil.getRequestHeaderInfo(Constants.USER_NAME);
            userName = userName == null || userName.isEmpty() ? "System" : userName;
            entity.setCreatedBy(userName);
            entity.setUpdatedBy(userName);
            entity.setExtendField1(entity.getYearTarget().toString());
            entity.setExtendField2(entity.getMonthTarget().toString());
            baseMapper.addRuleAmortization(entity);
        } catch (Exception ex) {
            return Response.error(String.join(":", "添加失败:", ex.getMessage()));
        }
        return Response.success("添加成功");
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Response insertBatchRuleAmortization(RuleAmortization entity) {
        try {
            RuleAmortization searchInfo = new RuleAmortization();
                searchInfo.setYearSource(entity.getYearSource());
                searchInfo.setMonthSource(entity.getMonthSource());
            List<RuleAmortization> sourceList = getRuleAmortization(searchInfo);
            if (CollectionUtils.isEmpty(sourceList)) {
                return Response.error("继承失败,没有找到对应规则下的数据");
            }

            Date currDate = new Date();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            entity.setCreatedOn(sdf.format(currDate));
            entity.setUpdatedOn(sdf.format(currDate));

            //清除当前月记录
            baseMapper.deleteByYearMonth(entity.getYearTarget(),entity.getMonthTarget());
            int total = sourceList.size();//为防止插入字符超过最大数,分批插入
            int fromIndex = 0;
            List<RuleAmortization> insertList = new ArrayList<>();
            for (int i = 0; i < sourceList.size(); i++) {
                RuleAmortization item = sourceList.get(i);
                item.setExtendField1(String.valueOf(entity.getYearTarget()));
                item.setExtendField2(String.valueOf(entity.getMonthTarget()));
                item.setCreatedOn(sdf.format(currDate));
                item.setUpdatedOn(sdf.format(currDate));
                String userName = HttpUtil.getRequestHeaderInfo(Constants.USER_NAME);
                userName = userName == null || userName.isEmpty() ? "System" : userName;
                item.setCreatedBy(userName);
                item.setUpdatedBy(userName);
                fromIndex++;
                insertList.add(item);
                if (fromIndex % 20 == 0 && fromIndex <= total) {
                    baseMapper.insertBatchRuleAmortization(insertList);
                    insertList = new ArrayList<>();
                }
            }
            if (CollectionUtils.isNotEmpty(insertList)) {
                baseMapper.insertBatchRuleAmortization(insertList);
            }

        } catch (Exception ex) {
            return Response.error(String.join(":", "继承失败:", ex.getMessage()));
        }
        return Response.success("继承成功");
    }

    @Override
    public Response modifyRuleAmortization(RuleAmortization entity) {
        if (entity.getId() == 0) {
            return Response.error("主键不能为空");
        }
        try {
            Date currDate = new Date();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:hh:ss");
            entity.setUpdatedOn(sdf.format(currDate));
            String userName = HttpUtil.getRequestHeaderInfo(Constants.USER_NAME);
            userName = userName == null || userName.isEmpty() ? "System" : userName;
            entity.setUpdatedBy(userName);
            baseMapper.modifyRuleAmortization(entity);
        } catch (Exception ex) {
            return Response.error(String.join(":", "修改失败", ex.getMessage()));
        }
        return Response.success("修改成功");
    }

    @Override
    public Response batchDeleteRuleAmortization(List<Integer> ids) {
        try {
            baseMapper.batchDeleteRuleAmortization(ids);
        } catch (Exception ex) {
            return Response.error("删除失败");
        }
        return Response.success("删除成功");
    }

}
